﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.Office.Interop.Excel;
using TecExtensions;

namespace TecBaseObjects.Base {
    public class JobSheet {

        #region Members

        private Worksheet mySheet;

        #endregion

        #region Contructor

        public JobSheet(Worksheet sh) {
            this.mySheet = sh;
            SetRowNumbers();
        }

        #endregion

        #region Properties

        public int ZeileSummeArbeitszeit {
            get;
            set;
        }

        public int ZeileSummeLeistungen {
            get;
            set;
        }

        #endregion

        #region Methods

        private void SetRowNumbers() {
            bool weiter = false;
            int startRow = 3;
            while (!weiter & startRow < 5000) {
                Range r1 = mySheet.Cells[startRow, "B"];
                Range r2 = mySheet.Cells[startRow, "C"];

                string t1 = Extensions.Nz(Convert.ToString(r1.Value), "");
                string t2 = t1 + Extensions.Nz(Convert.ToString(r2.Value), "");

                if (t2 != "") {
                    if (t2.ToUpper().Contains("ARBEIT")) {
                        this.ZeileSummeArbeitszeit = startRow;
                    }
                    if (t2.ToUpper().Contains("LEISTUNGEN")) {
                        this.ZeileSummeLeistungen = startRow;
                        weiter = true;
                    }
                }
                startRow++;
            }
        }

        #endregion

        #region Functions

        public int GetRowForWorkDay(DateTime date) {
            int ret = 0;
            int startRow = 3;
            bool weiter = false;

            while (!weiter && startRow < this.ZeileSummeArbeitszeit) {
                Range r1 = mySheet.Rows[startRow, Missing.Value];
                Range r2 = mySheet.Rows[startRow + 1, Missing.Value];
                TimeRow tr = new TimeRow(r1);
                TimeRow nextRow = new TimeRow(r2);
                if ((date.Date == tr.JobDate.Date) && (tr.JobDate.Date != nextRow.JobDate.Date)) {
                    return tr.RowNumber;
                }
                startRow++;
            }
            return ret;
        }

        #endregion

    }
}
